/*---------------------------------------------------------------------------*/
#include <QtGui>
#include "Window.h"
#include "MenuBox.h"
#include "Utils.h"
#include "Config.h"
#include "FilterBox.h"
#include "FileNode.h"
#include "BrowserBox.h"
#include "FilterBox.h"
/*---------------------------------------------------------------------------*/
Window::Window(char *path)
{
	// main layout
	QVBoxLayout *mainLayout = new QVBoxLayout();
	
	// widgets
	

	statusBar = new QStatusBar();
	statusBar->showMessage("Welcome...");
	list = new BrowserBox();
	menuBox = new MenuBox(list);		
	filterBox = new FilterBox(list);
	
	
	
	// add widgets to layout
	mainLayout->setSpacing(1);
	mainLayout->addLayout(menuBox);
	mainLayout->addWidget(list);
	mainLayout->addWidget(filterBox);
	mainLayout->addWidget(statusBar);
	// set general properties
	setLayout(mainLayout);
	setWindowTitle(tr("gfm"));
	paintEngine();
	
	resize(800,600);
	mainLayout->setContentsMargins(1,1,1,1);
	
	// connections
	connect( list, SIGNAL( directoryChanged(FileNode*) ), this, SLOT( directoryChanged(FileNode*) ));
	connect( list, SIGNAL( typing(QKeyEvent *) ), this, SLOT( userTyping(QKeyEvent *) ));
	connect( filterBox, SIGNAL( closed() ), this, SLOT( filterClosed() ));
	connect( filterBox, SIGNAL( changed(string) ), this, SLOT( filterChanged(string) ));
	
	// load config
	string stemp = getenv("HOME");
	stemp += "/.gfm";
	Config::loadConfig(stemp.c_str());
	
	
	list->fillFromCurrentNode();
	if( path ) 
		if( Utils::isDirectory(path) ) 
			list->goTo( path );
			
	list->setFocus(Qt::TabFocusReason);	
	
	// restore geometry
	QSettings settings("gogoprog", "gfm");
	restoreGeometry(settings.value("geometry").toByteArray());	
}
/*---------------------------------------------------------------------------*/
Window::~Window()
{
	Config::unloadConfig();
}
/*---------------------------------------------------------------------------*/
void Window::directoryChanged(FileNode *fn)
{	
	// address bar
	menuBox->setAddressBar( fn->getAbsolutePath().c_str() );
	
	// status bar
	string str =  Utils::formatSize( fn->getChildsSize(),1 ) + " = " + Utils::toString(fn->getChildsCount()) + " files in " + fn->getAbsolutePath();
	statusBar->showMessage( str.c_str() );
		
}
/*---------------------------------------------------------------------------*/
void Window::userTyping(QKeyEvent *event )
{	
	filterBox->setFocus(event);		
}
/*---------------------------------------------------------------------------*/
void Window::filterClosed()
{	
	list->setFocus(Qt::TabFocusReason);		
	list->setFilter("*");
}
/*---------------------------------------------------------------------------*/
void Window::filterChanged(string str)
{		
	list->setFilter(str);
}
/*---------------------------------------------------------------------------*/
void Window::paintEvent(QPaintEvent *event)
{
/*
    QPainter painter(this);
    QColor semiTransparentColor;
    semiTransparentColor.setAlpha(0.5f);
    painter.fillRect(rect(), semiTransparentColor);
*/
}
/*---------------------------------------------------------------------------*/  
void Window::closeEvent ( QCloseEvent * event )
{
	//DEBUGMSG("Window::closeEvent()");
	QSettings settings("gogoprog", "gfm");
	settings.setValue("geometry", saveGeometry());
	QWidget::closeEvent(event);
}
/*---------------------------------------------------------------------------*/
